package com.example.blog.controller;

import com.example.blog.dto.*;
import com.example.blog.dto.base.IdRequestDto;
import com.example.blog.entity.LiveInfo;
import com.example.blog.entity.LiveUser;
import com.example.blog.response.*;
import com.example.blog.service.LiveInfoService;
import com.example.blog.service.LiveUserService;
import com.example.blog.util.ParamUtil;
import com.example.blog.util.RequestUtil;
import com.example.blog.util.ToolsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Admin
 */
@RestController
@Api(value = "直播信息接口",tags = "直播信息接口")
@RequestMapping("/liveInfo")
public class LiveInfoController {

    @Resource
    private LiveInfoService liveInfoService;

    @Resource
    private LiveUserService liveUserService;

    @Resource
    private GenUserSig genUserSig;

    @PostMapping("/addLiveInfo")
    @ApiOperation("添加直播信息接口")
    public ResponseModelDto addLiveInfo(@RequestBody @Validated AddLiveInfoReqDto reqDto){
        String userId = RequestUtil.getUserId();
        LiveUser liveUser = liveUserService.selectUserById(userId);
        if(ParamUtil.empty(liveUser)){
            throw new CommonException("未查询到该用户");
        }
        reqDto.setRoomNo(liveUser.getRoomNo());
        if(ObjectUtils.notEqual(liveUser.getRoomNo(),reqDto.getRoomNo())){
            throw new CommonException("只能给自己的直播间添加信息");
        }
        reqDto.setUserId(liveUser.getId());
        reqDto.setUserName(liveUser.getName());
        if(liveInfoService.addLiveInfo(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("添加直播失败");
        }
    }

    @PostMapping("/updateLiveInfo")
    @ApiOperation("修改直播信息")
    public ResponseModelDto updateLiveInfo(@RequestBody @Validated UpdateLiveInfoReqDto reqDto){
        String userId = RequestUtil.getUserId();
        LiveUser liveUser = liveUserService.selectUserById(userId);
        if(ParamUtil.empty(liveUser)){
            throw new CommonException("未查询到该用户");
        }
        LiveInfo liveInfo = liveInfoService.selectLiveInfoById(reqDto.getId());
        if(ParamUtil.empty(liveInfo)){
            throw new CommonException("未查询到该直播信息");
        }
        if(ObjectUtils.notEqual(liveUser.getId(),liveInfo.getUserId())){
            throw new CommonException("只能修改自己的直播信息");
        }
        reqDto.setUserId(liveUser.getId());
        reqDto.setUserName(liveUser.getName());
        if(liveInfoService.updateLiveInfo(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("修改直播失败");
        }
    }

    @PostMapping("/deleteLiveInfo")
    @ApiOperation("删除直播信息")
    public ResponseModelDto deleteLiveInfo(@RequestBody @Validated IdRequestDto reqDto){
        String userId = RequestUtil.getUserId();
        LiveInfo liveInfo = liveInfoService.selectLiveInfoById(reqDto.getId());
        if(ParamUtil.empty(liveInfo)){
            throw new CommonException("未查询到该直播信息");
        }
        if(ObjectUtils.notEqual(userId,liveInfo.getUserId())){
            throw new CommonException("只能删除自己的直播间信息");
        }
        if(liveInfoService.deleteLiveInfo(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("删除直播信息失败");
        }
    }

    @GetMapping("/selectLiveInfo")
    @ApiOperation("分页查询直播信息")
    public PageableResponseModelDto<QueryLiveInfoResDto> selectLiveInfo(QueryLiveInfoReqDto reqDto){
        return ResponseModels.page2ResponseModel(liveInfoService.selectLiveInfo(reqDto));
    }

    @GetMapping("/getLiveInfo")
    @ApiOperation("查询直播详情")
    public ResponseModelDto<LiveInfoResDto> getLiveInfoById(String id){
        LiveInfo liveInfo = liveInfoService.selectLiveInfoById(id);
        LiveInfoResDto resDto = ToolsUtil.convertType(liveInfo,LiveInfoResDto.class);
        resDto.setUserSig(genUserSig.genUserSig(liveInfo.getRoomNo()).getData());
        resDto.setShareUserSig(genUserSig.genUserSig("share_"+liveInfo.getRoomNo()).getData());
        return ResponseModels.ok(resDto);
    }

    @PostMapping("/closeLive")
    @ApiOperation("关闭直播")
    public ResponseModelDto closeLive(@RequestBody @Validated CloseLiveReqDto reqDto){
        if (liveInfoService.closeLive(reqDto)){
            return ResponseModels.ok();
        }else{
            throw new CommonException("关闭直播失败");
        }
    }

}
